home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / utility / 100 / tutorpas.eng < prev    next >
Encoding:
Text File  |  1987-01-16  |  10.6 KB  |  299 lines

  1. 0660103030566
  2. 1
  3. 2Page #
  4. 9[....................................................]
  5. Ç
  6. ====================SECONDLESSONIN====================
  7.  
  8. àPASCAL
  9. Ç
  10.      =================BySERGEVAILLANCOURT==================
  11.  
  12.  
  13.      Hello,everyone!!
  14.  
  15.      I'mhappytobebackwithyoutogiveyouparttwoofthis
  16. Pascaltutorial.Ihopethefirstpartwasusefulandthatyour
  17. firstprogramsworkedsuccessfully.Mostimportantly,don'tforget
  18. toleavemeamessageontheFASTERBBSifyouhaveanyproblems.
  19. Itwillbeapleasureformetohelpyouifpossible.
  20.  
  21.      But,enoughidlechattingandlet'sgetstartedwiththe
  22. matrixvariables.
  23.  
  24.  
  25.      üMATRIXVARIABLES
  26. Ç================
  27.  
  28.      Amatrixvariableisavariablecontainingmanyvalues,each
  29. oneofthesebeingindexed.
  30.  
  31.      Supposewewanttosavesomestudentsgrades.Afterhaving
  32. learnedlesson1,weshouldwritethefollowingprogram:
  33.  
  34.      àPROGRAMÇscoreà(INPUT);Ç
  35.      (*rememberthatwordsinCapitals/ItalicsarePascal
  36. reservedwords*)
  37.      àVARÇn1,n2,n3,n4,n5,n6,n7.n8:àINTEGER;Ç
  38. (*Weassumethestudenthas8scorestograde*)
  39. àBEGINÇ
  40.           àREADLNÇ(n1,n2,n3,n4,n5,n6,n7,n8);
  41.      àEND.Ç
  42.  
  43.                     (program8)
  44.  
  45.      Buttheprogramwouldbemuchsimplerwithamatrixvariable:
  46.  
  47.      àPROGRAMÇscore2à(INPUT);äÇ
  48.      àVARÇscores:àARRAYäÇ[1..8]àOFINTEGER;Ç
  49.           (*declarationofamatrixtypevariable*)
  50.           i:àINTEGER;Ç(*loopingindex*)
  51.      àBEGINÇ
  52.           àFORÇi:=1àTOÇ8àDOREADLNÇ(scores[i]);
  53.      àEND.Ç
  54.  
  55.                     (program9)
  56.  
  57.      YouwillnoticethattheàFORÇloopindexalsobecomesthe
  58. matrixvariable'sindex.
  59.  
  60.      Declaringamatrixvariablemustalwaysadheretothe
  61. followingsyntax:
  62.  
  63.                               àARRAYÇ
  64.                               |
  65.                               à[Ç
  66.                Sizeofthematrix(typeofindex)
  67.                               à]Ç
  68.                                 |
  69.                               àOFÇ
  70.                               |
  71.                     Typeofmatrixvariable
  72.  
  73.                      (figure9)
  74.  
  75.      Theindexcarriesthesizeofthematrix.Inthepreceding
  76. example,thematrixhas8slots,numberedfrom1to8.Thisindex
  77. hastobeoneofthefollowingtypes:
  78.  
  79.      -SCALAR(seebelow)
  80.      -CHARACTER(àCHARÇ)
  81.      -BOOLEAN(àBOOLEANÇ)
  82.      -INTERVAL
  83.  
  84.      TheINTERVALtypeis,infact,a"sub-type",likeinour
  85. example,1..8isanintervaloftypeàINTEGERÇ.It'sthemost
  86. oftenusedforindexingamatrix.Thedeclarationofaninterval
  87. typehasthefollowingaspect.
  88.  
  89.                               constant
  90.                                  |
  91.                               ..
  92.                               |
  93.                               constant
  94.  
  95.                     (figure10)
  96.  
  97.      Understandherethatanintervalofrealnumbersisnot
  98. acceptablebecausebetweentwogivennumbers,theremaybean
  99. infinityofothernumbers.
  100.  
  101.      Constantsmaybedefinedintheconstantsdeclaration.
  102. Example:
  103.  
  104.      àPROGRAMÇscore3à(INPUT);
  105.      CONSTÇmaxscore=8;
  106.      àVARÇscore:àARRAYÇ[1..maxscore]àOFINTEGER;
  107. Ç          i:àINTEGER;
  108.      BEGIN
  109.         FORÇi:=1àTOÇmaxscoreàDOREADLNÇ(score[i]);
  110.      àEND.Ç
  111.  
  112.                     (program10)
  113.  
  114.      Wemayalsodeclarethetypeofindex:
  115.  
  116.      àPROGRAMÇscore4à(INPUT);
  117.      CONSTÇmaxscore=8;
  118.      àTYPEÇrange:1..maxscore;
  119.      àVARÇscore:àARRAYÇ[range]àOFINTEGER;Ç
  120.           i:àINTEGER;Ç
  121.      àBEGINÇ
  122.           àFORÇi:=1àTOÇmaxscoreàDOREADLNÇ(score[i]);
  123.      àEND.Ç
  124.  
  125.                     (program11)
  126.  
  127.      Thisway,ifduringthenextsession,thescoreofeach
  128. studentneedstobecompiledonatotalof7insteadof8,the
  129. onlythingyouwouldneedtodoistochangethevalueof
  130. maxscore.
  131.  
  132.      Asforthematrix,itmaybeofanytype.Thisbecomes
  133. interestingifourvariable,insteadofbeingassignedtoonlyone
  134. student,shouldcontainthescoresofthewholeclass.Thus,the
  135. matrix'stypewouldbeanothermatrix.
  136.  
  137.      àPROGRAMÇclassà(INPUT);
  138.      CONSTÇmaxscore=8;(*numberofscoresperstudent*)
  139.           maxstudent=35;(*numberofstudentsperclass*)
  140.      àTYPEÇscorerange:1..maxscore;
  141.           studentrange:1..maxstudent;
  142.           onestudent:àARRAYÇ[scorerange]àOFINTEGER;äÇ
  143. class:àARRAYÇ[studentrange]àOFÇonestudent;
  144.      àVARÇscore:class;(*2dimensionvariableoftypeclass*)
  145.           i,j:àINTEGER;Ç(*loopindex*)
  146.      àBEGINÇ
  147.           àFORÇi:=1àTOÇmaxstudentàDOÇ
  148.                àFORÇj:=1àTOÇmaxscoreàDOÇ
  149.                     àREADLNÇ(score[i,j]);
  150.      àEND.Ç
  151.  
  152. à                    Ç(program12)
  153.  
  154.      Wehadinfrontofus,inthelastprogram,atwo-dimension
  155. array.
  156.  
  157.      "àARRAYÇ[type1]àOFARRAYÇ[type2]àOFÇtype3"isthesame
  158. thingas"àARRAYÇ[type1,type2]àOFÇtype3".
  159.  
  160.      WeunderstandherethatinàREADLNÇ(score[i,j]),the"i"is
  161. thefirstindex(firstdimension)whilethe"j"isthesecond
  162. index(seconddimension).Therearenolimitstothenumberof
  163. dimensions(1,2,3,4,etc...)Onlyyourcompileroryourcomputer's
  164. availablememorywillphysicallylimityou.However,itisrare
  165. indeedtoneedmorethanthreedimensions.
  166.  
  167.      üCHARACTERSTRINGS
  168. Ç=================
  169.  
  170.      Acharacterstringisinfactnothingmorethanamatrixof
  171. characters.Example:
  172.  
  173.      àARRAYÇ[1..30]àOFCHARÇ
  174.  
  175.      Inthisexample,weobtaina30characterstring.
  176.  
  177.      àPROGRAMÇyournameà(INPUT,OUTPUT);
  178.      CONSTÇstringlenght=30;
  179.                (*maximumlenghtofthecharacterstring*)
  180.      àTYPEÇstring30:àARRAYÇ[1..stringlenght]àOFCHAR;
  181.      VARÇname:string30;
  182.      àBEGINÇ
  183.           àWRITELNÇ('What'syourname?')
  184.           àREADLNÇ(name);
  185. à          WRITELNÇ('Goodday',name);
  186. à     END.
  187. Ç
  188.                      (program13)
  189.  
  190.  
  191.      Insidethecomputer,onecharacteroccupiesonebyteof
  192. memory.Thus,inthelastexample,thestringrequires30bytesof
  193. storage.However,it'spossibletoreleasealmosthalfthememory
  194. withtheàPACKÇinstruction.
  195.  
  196.      Thisinstructionallowsustostoreeachcharacterinone
  197. nybbleinsteadofabyte,whichsavesalotofmemoryinthe
  198. treatmentofacharactermatrix.Tobetterunderstand,readthe
  199. followingprogramwhichfetchesallthestudentsscoreswhich
  200. namesappearinsuccessiononthescreen.
  201.  
  202.      àPROGRAMÇclass2à(INPUT,OUTPUT);
  203. Ç     àCONSTÇmaxscore=8;
  204.           maxstudent=35;
  205.      àTYPEÇscorerange:1..maxscore;
  206.           studentrange:1..maxstudent;
  207.           string30:àPACKEDARRAYÇ[1..30]àOFCHAR;
  208. Ç          onestudent:àARRAYÇ[studentrange]àOFINTEGER;
  209. Ç     àVARÇscores:àARRAYÇ[studentrange]àOFINTEGER;Ç
  210.                     (*matrixofscores*)
  211.           names:àARRAYÇ[studentrange]àOFÇstring30;
  212.                     (*matrixofnames*)
  213.           i,j:àINTEGER;
  214. Ç     àBEGINÇ
  215.           àFORÇi:=1àTOÇmaxstudentàDOÇ
  216.           àBEGIN
  217.                WRITEÇ(names[i]);(*writesthename*)
  218.                àFORÇj:=1àTOÇmaxscoreàDOÇ
  219.                     àREADÇ(scores[i,j]);
  220.                     (*readsthestudentsscores*)
  221.                àWRITELN;Ç(*returnsthecursortothenextline*)
  222.           àEND;Ç(*fori*)
  223. à     END.
  224. Ç
  225.                     (program14)
  226.  
  227.      àPACKÇandàUNPACKÇ
  228.      ================
  229.  
  230.      Asmentionedabove,àPACKäÇallowsyoutocompressthecontents
  231. ofamatrix.Notonlyofacharactermatrixbutalsoothertypes.
  232. Infactanoncompactedmatrixreservesatleastonebyteper
  233. entry,dependingonthetypeofmatrix.Ifitisamatrixof
  234. booleanvalues,itwillreserveonebyteperentrywhileonlythe
  235. firstbitofthebyteissignificant.Ifwepackthismatrix,we
  236. willbeabletostore8valuesintoasinglebyte,asavingof
  237. approximately90%ofmemory.
  238.  
  239.      àUNPACKÇhastheoppositeeffect,i.e.bringsvaluesbackto
  240. theiroriginalstate.
  241.  
  242.      Topackamatrixofvalues,usethefollowingsyntax:
  243.  
  244.      àPACKÇ(a,b,c);
  245.  
  246.      Where"a"isthematrixvariable;
  247.           "b"istheresult(compactedmatrixvariable);
  248.            "c"isanintegerholdingtheindexofthefirst    
  249.                componentof"a"tooccurintheoperation.
  250.  
  251.      Tounpackamatrix,youmustusethefollowingsyntax:
  252.  
  253. à     UNPACKÇ(c,a,b);
  254.  
  255.      Where"a","b",and"c"carrythesamemeaningasinthe
  256. syntaxofàPACKä.
  257. Ç
  258.      üSCALARTYPESÇ
  259.      ============
  260.  
  261.      Bydefinition,thescalartypeisanarrayofdefinedvalues
  262. thisvariabletypemayassume.
  263.  
  264.      Forexample,theàINTEGERÇtypeisascalartype,theàCHARÇ
  265. typeisonealso.TypeàREALäÇhowever,isnot.
  266.  
  267.      Theprogrammermaydefinehis/herownscalartypes.Eachnew
  268. typehasanidentifierandisdefinedbyalistofvaluesthat
  269. variablemaycarry.
  270.  
  271.      Example:àTYPEÇcivilstatus=(bachelor,married,divorced,
  272. separated,widow);
  273.  
  274.      Andwemaythendefineavariableofthistype:
  275.  
  276.           àVARÇalain:civilstatus;
  277.  
  278.      Thealainvariablemaynowcarryanyvalueofthecivilstatus
  279. type,beitbachelor,married,divorced,separated,orwidow.
  280.  
  281.      Example:alain:=bachelor;
  282.  
  283.      Thevaluescontainedinthelistdefiningascalartype
  284. variablearenotcharacterstrings.Itisimpossibletoreador
  285. printthem.However,thesevaluesmayinterveneinexpressionsand
  286. maybecompared.
  287.  
  288.      Itisimpossibletohavethesamevalueappearintwo
  289. differenttypedefinitions.
  290.  
  291.      Example:àTYPEÇfirstnames=(julie,serge,alain,denise);
  292.                     fabrics=(cotton,wool,serge);
  293.  
  294.      Thescalartypescreatedbytheprogrammerareusedtoclose
  295. thegapbetweenprogrammingandhumanlanguages.
  296.  
  297.      Untilthen,takecareandhappyprogramming!
  298.  
  299.